package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    //新增套餐信息  需要关联检查组
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealMapper.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckGroup(setmealId, checkgroupIds);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealMapper.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Setmeal findById(Integer id) {
     return    setmealMapper.findById(id);
    }

    @Override
    public List<Setmeal> getSetmeal() {
        List<Setmeal> setmealList = setmealMapper.getSetmeal();
        return setmealList;
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //操作套餐基本信息  操作套餐 setmeal表
        setmealMapper.edit(setmeal);
        //清理 当前套餐 关联的检查组  操作中间关系表
        setmealMapper.deleteAssociation(setmeal.getId());
        //重新建立当前检查组和套餐的关联关系
        setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);

    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        return setmealMapper.findCheckGroupIdsBySetmealId(id);
    }

    @Override
    public void deleteById(Integer id) {
    /*    long count = setmealMapper.findCountBySetmealId(id);
        if (count>0) {
            throw new RuntimeException("当前套餐和检查组存在关系,不能直接删除");
        }*/

        setmealMapper.deleteAssociation(id);
        setmealMapper.deleteById(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealMapper.findSetmealCount();
    }


    public void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealMapper.setSetmealAndCheckGroup(map);

            }
        }
    }






}
